import { ref, watch } from 'vue';
import { defineStore } from 'pinia';

export const useLocalStore = defineStore('local', () => {
  // 存储ip天气
  const localWeatherp = ref([]);

  // 存储搜索天气
  const searchWeatherp = ref([]);

  // ⚠️存放添加的城市
  const cityNamep = ref([]);
  // 监听 cityNamep 变化，保存到 localStorage
  watch(
    cityNamep,
    (newVal) => {
      localStorage.setItem('cityNamep', JSON.stringify(newVal));
    },
    { deep: true }
  ); 

  /*   // 从本地获取数据
  localCity.value = JSON.parse(localStorage.getItem('localCity'));
  localWeather.value = JSON.parse(localStorage.getItem('localWeather'));
 */
  /* // 城市信息保存到pinia得方法
  const updataCity = (data) => {
    localCityp.value = data;
  }; */

  // 城市信息保存到pinia得方法
  const updataWip = (data) => {
    localWeatherp.value = data;
  };
  // 搜索天气 保存到inia得方法
  const updataWsearch = (data) => {
    searchWeatherp.value = data;
  };
  // ⚠️添加 城市
  const addCityname = (data) => {
     cityNamep.value.push(data);
  };

  return {
    localWeatherp,
    searchWeatherp,
    cityNamep,
    updataWip,
    updataWsearch,
    addCityname,
  };
});
